//
//  NewFeatureViewController.swift
//  weiboLQ
//
//  Created by 半缘君 on 15/12/9.
//  Copyright © 2015年 banyuanjun.cn. All rights reserved.
//

import UIKit
import SnapKit
private let NewFeatureReuseIdentifier = "NewFeatureCell"
private let NewFeatureCellCount = 4

class NewFeatureViewController: UICollectionViewController {
    
    
    init () {
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        super.init(collectionViewLayout: layout)
        
        //分页
        collectionView?.pagingEnabled = true
        
        //弹簧
        collectionView?.bounces = false
        //取消水平滚动条
        collectionView?.showsHorizontalScrollIndicator = false
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(collectionViewLayout: UICollectionViewFlowLayout())
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Register cell classes
        self.collectionView!.registerClass(NewFeatureCollectionCell.self, forCellWithReuseIdentifier: NewFeatureReuseIdentifier)

        // Do any additional setup after loading the view.
    }


    // MARK: UICollectionViewDataSource


    
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return NewFeatureCellCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(NewFeatureReuseIdentifier, forIndexPath: indexPath) as! NewFeatureCollectionCell
//        let imageN = "new_feature_\(indexPath.item + 1)"
//        let iv = UIImageView(image: UIImage(named: imageN))
//        iv.frame = UIScreen.mainScreen().bounds
//        cell.contentView.addSubview(iv)
        cell.imageIndex = indexPath.item
//        print(cell.contentView.subviews.count)
        return cell
    }

    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        let page = Int(scrollView.contentOffset.x / scrollView.bounds.width)
        if page != NewFeatureCellCount - 1 {
            return
        }
        
        let cell = collectionView?.cellForItemAtIndexPath(NSIndexPath(forItem: page, inSection: 0)) as! NewFeatureCollectionCell
        cell.showStartButtonAnimation()
}
}

private class NewFeatureCollectionCell:UICollectionViewCell{
    
    //    var btn:UIButton?
    lazy var iconView:UIImageView = UIImageView()
    lazy var startBtn:UIButton = UIButton(titile: "开始体验",backGroundImage: "new_feature_finish_button")
//        startBtn.setTitle("开始体验", forState: UIControlState.Normal)
//        startBtn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
//        startBtn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)

    
    override init(frame:CGRect) {
        super.init(frame: frame)
        setUpUI()
        //        self.bringSubviewToFront(btn!)
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(frame: CGRectMake(0, 0, 0, 0))
        setUpUI()
    }
    
    func setUpUI () {
        contentView.addSubview(iconView)
        
        contentView.addSubview(startBtn)
        
        iconView.frame = bounds
        startBtn.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(110)
            make.height.equalTo(35)
            make.width.equalTo(90)
        }
        
        self.contentView.addSubview(startBtn)
        startBtn.addTarget(self, action: "clickStartBtn", forControlEvents: UIControlEvents.TouchUpInside)
    }
    
    var imageIndex:Int? {
        didSet {
            let imageN = "new_feature_\(imageIndex! + 1)"
            iconView.image = UIImage(named: imageN)
            if imageIndex == NewFeatureCellCount - 1 {
                
                startBtn.hidden = false
            } else {
                startBtn.hidden = true
            }
            
        }
    }
    
    func showStartButtonAnimation () {
        startBtn.hidden = false
        startBtn.transform = CGAffineTransformMakeScale(0, 0)
        UIView.animateWithDuration(1.6,
            delay: 0.1,
            usingSpringWithDamping: 0.5,
            initialSpringVelocity: 10,
            options: [],
            animations: { () -> Void in
                self.startBtn.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                
        }
    }
}
extension NewFeatureCollectionCell {
    @objc private func clickStartBtn () {
        print("开始体验")
        NSNotificationCenter.defaultCenter().postNotificationName(LQChangeRootViewController, object: "main")
    }
}